/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.market;

import cz.fi.muni.diplomka.common.enumeration.MovementType;
import cz.fi.muni.diplomka.common.enumeration.ResourceType;
import cz.muni.fi.diplomka.servicelayer.dto.City;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityResourcesManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.MarketManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityResourcesManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.MarketManagerImpl;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author Jakub Kutil
 */
public class NewCaravanPage extends WebPage {

    private MarketManager marketManager = new MarketManagerImpl();
    private CityResourcesManager cityResourcesManager = new CityResourcesManagerImpl();
    private CityManager cityManager = new CityManagerImpl();
    private String selectedCity = "";
    private String selectedResource = "Gold";
    private List<String> cities;

    public NewCaravanPage(PageParameters parameters) {

        List<String> RESOURCE = new ArrayList<>();

        for (ResourceType r : ResourceType.values()) {
            RESOURCE.add(r.toString());
        }

        final Long id;

        add(new FeedbackPanel("feedback"));

        if (parameters.containsKey("id")) {
            id = Long.parseLong(parameters.getString("id"));

            PageParameters p = new PageParameters();
            p.add("id", id.toString());

            add(new LinkPanel("linkPanel", p));


            cities = new ArrayList<>();
            for (City c : cityManager.getCitiesByPlayerId(id)) {
                cities.add(c.getName());
            }
            if (parameters.containsKey("cityName")) {
                selectedCity = parameters.getString("cityName");
            }

            if (selectedCity.equals("")) {
                selectedCity = cities.get(0).toString();
            }

            p.add("cityName", selectedCity);

            add(new MarketPanel("marketPanel", p));

            final DropDownChoice<String> city = new DropDownChoice<>("city",
                    new PropertyModel<String>(this, "selectedCity"), cities);

            final RequiredTextField<String> to = new RequiredTextField<>("to", Model.of(""));

            final DropDownChoice<String> from = new DropDownChoice<>("from",
                    new PropertyModel<String>(this, "selectedCity"), cities);

            final DropDownChoice<String> resource = new DropDownChoice<>("resource",
                    new PropertyModel<String>(this, "selectedResource"), RESOURCE);

            final RequiredTextField<String> count = new RequiredTextField<>("count", Model.of(""));
            count.add(new CountValidator());
            
            Form<?> form = new Form<Void>("form") {
                @Override
                protected void onSubmit() {
                    selectedCity = cities.get(Integer.parseInt(city.getValue()));
                    PageParameters pageParameters = new PageParameters();
                    if (cityManager.getCityByName(to.getValue()).getId() == null) {
                        error("City to send not found");
                    } else {
                        if (cityResourcesManager.getResourceCount(cityManager.getCityByName(selectedCity).getId(),
                                ResourceType.valueOf(selectedResource)) > Integer.parseInt(count.getValue())) {

                            marketManager.sendResources(cityManager.getCityByName(selectedCity).getId(), cityManager.getCityByName(to.getValue()).getId(), ResourceType.valueOf(selectedResource), Integer.parseInt(count.getValue()), MovementType.Support);
                            pageParameters.add("id", id.toString());
                            pageParameters.add("cityName", selectedCity);
                            setResponsePage(NewCaravanPage.class, pageParameters);
                        } else {
                            error("You do not have enough to send specified amount");
                        }
                    }
                }
            };
            
            add(form);
            form.add(to);
            form.add(from);
            form.add(resource);
            form.add(count);
        }
    }
}
